package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.dto.NursingLevelDTO;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.vo.NursingLevelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
/**
 * 护理等级Service业务层处理
 * 
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevelVO selectNursingLevelById(Integer id)
    {
        NursingLevel nursingLevel = this.getById(id);
        return BeanUtil.toBean(nursingLevel, NursingLevelVO.class);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevelDTO 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevelDTO nursingLevelDTO)
    {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDTO, NursingLevel.class);
        boolean flag = this.save(nursingLevel);
        deleteCache();
        return flag ? 1 : 0;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevelDTO 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevelDTO nursingLevelDTO)
    {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDTO, NursingLevel.class);
        boolean flag = this.updateById(nursingLevel);
        deleteCache();
        return flag ? 1 : 0;
    }

/*    *//**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     *//*
    @Override
    public int deleteNursingLevelByIds(Integer[] ids)
    {
        return this.removeBatchByIds(Arrays.asList(ids)) == true ? 1 : 0;
    }*/

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Integer id)
    {
        boolean flag = this.removeById(id);
        deleteCache();
        return flag ? 1 : 0;
    }

    /**
     * 查询全部护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevel> listAll() {

        List<NursingLevel> nursingLevels = (List<NursingLevel>) redisTemplate.opsForValue().get(CacheConstants.NURSINGLEVEL_ALL);
        if (CollUtil.isNotEmpty(nursingLevels)) {
            return nursingLevels;
        }

        // 查询数据库
        nursingLevels = nursingLevelMapper.listAll();

        // 缓存数据到redis中
        redisTemplate.opsForValue().set(CacheConstants.NURSINGLEVEL_ALL, nursingLevels);

        return nursingLevels;
    }


    /**
     * 删除redis中的旧缓存
     */
    private void deleteCache() {
        redisTemplate.delete(CacheConstants.NURSINGLEVEL_ALL);
    }
}
